﻿using Engine.Audio;
using GameEntitySystem;

namespace Game
{
    public class CommandDemoModLoader : ModLoader
    {
        public SubsystemCommandDef m_subsystemCommandDef;

        public ComponentPlayer m_componentPlayer;

        public override void __ModInitialize()
        {
            ModsManager.RegisterHook("OnProjectLoaded", this);
        }

        public override void OnProjectLoaded(Project project)
        {
            m_subsystemCommandDef = project.FindSubsystem<SubsystemCommandDef>();
            m_componentPlayer = m_subsystemCommandDef.m_componentPlayer;
            SubsystemProjectiles subsystemProjectiles = project.FindSubsystem<SubsystemProjectiles>();
            SubsystemCommandBlockBehavior subsystemCmdBlockBehavior = project.FindSubsystem<SubsystemCommandBlockBehavior>();
			SubsystemCommand.AddFunction("launchdrop", delegate (CommandData commandData)    //发射投掷物
            {
                //launchdrop type:def-fire pos:0,0,0 id:46 vec3:5,5,5=生成并发射指定投掷物(pos:发射点;vec3:发射向量)
                if (commandData.Type == "def-fire")
                {
                    Point3 pos = m_subsystemCommandDef.GetOnePoint("pos", commandData);
                    int id = (int)commandData.GetValue("id");
                    Vector3 vec3 = (Vector3)commandData.GetValue("vec3");
                    subsystemProjectiles.AddProjectile(id, new Vector3(pos) + new Vector3(0.5f), vec3, Vector3.Zero, null);
                }
                return SubmitResult.Success;
            });
			SubsystemCommand.AddFunction("cmdtext", delegate (CommandData commandData)    //执行命令文本
            {
                //cmdtext type:default pos:0,0,0=获取指定命令方块的命令文本
                if (commandData.Type == "default")
                {
                    Point3 pos = m_subsystemCommandDef.GetOnePoint("pos", commandData);
                    CommandData data = subsystemCmdBlockBehavior.GetCommandData(pos);
                    if (data == null)
                    {
                        m_subsystemCommandDef.ShowSubmitTips("找不到命令方块数据");
                        return SubmitResult.Fail;
                    }
                    ClipboardManager.ClipboardString = data.Line;
                    m_subsystemCommandDef.ShowSubmitTips("已将命令文本复制到剪切板：\n" + data.Line);
                }
                //cmdtext type:execute f:cmdtext.txt=执行指定的命令文本
                else if (commandData.Type == "execute")
                {
                    string f = (string)commandData.GetValue("f");
                    Task.Run(delegate
                    {
                        Stream stream = m_subsystemCommandDef.GetCommandFileStream(f, OpenFileMode.ReadWrite);
                        StreamReader streamReader = new StreamReader(stream);
                        int i = 1;
                        string line = string.Empty;
                        string errorLine = string.Empty;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            try
                            {
                                CommandData data = new CommandData(commandData.Position, line);
                                data.TrySetValue();
                                if (!string.IsNullOrEmpty(data.Name))
                                {
                                    m_subsystemCommandDef.Submit(data.Name, data, false);
                                }
                                else
                                {
                                    errorLine += i + ",";
                                }
                            }
                            catch
                            {
                                errorLine += i + ",";
                            }
                            i++;
                        }
                        if (!string.IsNullOrEmpty(errorLine))
                        {
                            m_subsystemCommandDef.ShowSubmitTips("执行发生错误，已跳过，错误行：" + errorLine);
                        }
                        stream.Dispose();
                    });
                }
                return SubmitResult.Success;
            });
			SubsystemCommand.AddCondition("musicplay", delegate (CommandData commandData)  //音乐播放判定
            {
                //if:musicplay type:default f:再回首.wav=指定歌曲是否正在播放
                if (commandData.Type == "default")
                {
                    string f = (string)commandData.GetValue("f");
                    if (SubsystemCommandDef.m_commandMusic != null && SubsystemCommandDef.m_commandMusic.Sound != null)
                    {
                        if (SubsystemCommandDef.m_commandMusic.Name == f && SubsystemCommandDef.m_commandMusic.Sound.State == SoundState.Playing)
                        {
                            return SubmitResult.Success;
                        }
                    }
                }
                //if:musicplay type:play=歌曲是否正在播放
                else if (commandData.Type == "play")
                {
                    if (SubsystemCommandDef.m_commandMusic != null && SubsystemCommandDef.m_commandMusic.Sound != null)
                    {
                        if (SubsystemCommandDef.m_commandMusic.Sound.State == SoundState.Playing)
                        {
                            return SubmitResult.Success;
                        }
                    }
                }
                return SubmitResult.Fail;
            });
        }
    }
}